//
//  TakeListViewController.m
//  你的温实初
//
//  Created by lanou on 16/7/14.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "TakeListViewController.h"

@interface TakeListViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webview;

@property (nonatomic, strong) UIView *huview;

@end

@implementation TakeListViewController

- (UIView *)huview {
    if (!_huview) {
        _huview = [[UIView alloc] initWithFrame:CGRectMake(0, self.webview.frame.size.height-150, self.webview.frame.size.width, 150)];
        _huview.backgroundColor = [UIColor whiteColor];
    }
    return _huview;
}

- (UIWebView *)webview {
    if (!_webview) {
        _webview = [[UIWebView alloc] initWithFrame:self.view.frame];
        _webview.scrollView.bounces = NO;
        _webview.scrollView.bouncesZoom = NO;
        _webview.delegate = self;
    }
    return _webview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@", self.ID);
    [self.view addSubview:self.webview];
    [RequestManager fetchDataWithUrl:[NSString stringWithFormat:@"http://dxy.com/app/i/columns/article/single?id=%@", self.ID] Parameter:@{} Method:@"GET" Compelet:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        [self.webview loadHTMLString:bigDic[@"data"][@"items"][0][@"content"] baseURL:nil];
//        NSLog(@"%@", bigDic[@"data"][@"items"][0][@"content"]);
        NSString *str = bigDic[@"data"][@"items"][0][@"content"];
        NSArray *array = [str componentsSeparatedByString:@"丁香园"];
        [self.webview loadHTMLString:array[0] baseURL:nil];
    } updateUI:^{
        
    }];
    
//    [self.webview addSubview:self.huview];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *js = @"function imgAutoFit() { \
    var imgs = document.getElementsByTagName('img'); \
    for (var i = 0; i < imgs.length; ++i) {\
    var img = imgs[i];   \
    img.style.maxWidth = %f;   \
    } \
    }";
    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
    
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
